package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.member.OpinionEntity;
import com.dragon.modules.yuegou.excel.member.OpinionWorkbook;
import com.dragon.modules.yuegou.mapper.member.OpinionMapper;
import com.dragon.modules.yuegou.model.member.Opinion;
import com.dragon.modules.yuegou.model.system.Document;
import com.dragon.modules.yuegou.query.member.OpinionQuery;
import com.dragon.modules.yuegou.service.member.OpinionService;
import com.dragon.modules.yuegou.service.system.DocumentService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 意见反馈 服务实现
 *
 * @author pbuilder generated
 * @date 2022-03-18 14:10:11
 * @version 2.0
 */
@Service("memOpinionService")
public class OpinionServiceImpl
		extends ServiceImpl<OpinionMapper, OpinionEntity>
		implements OpinionService {

	@Autowired
	private DocumentService documentService;

	@Override
	public IPage<Opinion> search(PageRequest page, OpinionQuery query) {
		QueryWrapper<OpinionEntity> wrapper  = query.toQueryWrapper();
		wrapper.orderByDesc(OpinionEntity.COL_CREATED_TIME);
		return this.page(PageToResultUtils.resultToPage(page), wrapper);
	}

	@Override
	public Document exportOpinionDetail(OpinionQuery query) {
		List<Opinion> list = baseMapper.searchExport(query);
		OpinionWorkbook memberWorkbook = new OpinionWorkbook();
		List<OpinionWorkbook.OpinionRow> members = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Opinion member : list) {
				OpinionWorkbook.OpinionRow memberRow = new OpinionWorkbook.OpinionRow();
				memberRow.setContent(member.getContent());
				memberRow.setMobile(member.getMobile());
				memberRow.setCreatedTime(member.getCreatedTime());
				members.add(memberRow);
			}
		}
		memberWorkbook.setOpinion(members);
		return documentService.generateExcel("反馈记录表", memberWorkbook);
	}

	@Override
	public void emptyImg(String id) {
		baseMapper.emptyImg(id);
	}
}